package com.lbj.googleplay.googleplay100.ui.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;

import com.lbj.googleplay.googleplay100.R;
import com.lbj.googleplay.googleplay100.domain.AppInfo;
import com.lbj.googleplay.googleplay100.http.protocol.HomeDetailProtocol;
import com.lbj.googleplay.googleplay100.ui.holder.DetailAppinfoHolder;
import com.lbj.googleplay.googleplay100.ui.holder.DetailDesHolder;
import com.lbj.googleplay.googleplay100.ui.holder.DetailDownloadHolder;
import com.lbj.googleplay.googleplay100.ui.holder.DetailPicsHolder;
import com.lbj.googleplay.googleplay100.ui.holder.DetailSafeHolder;
import com.lbj.googleplay.googleplay100.ui.view.LoadingPage;
import com.lbj.googleplay.googleplay100.utils.UIUtils;

public class HomeDetailActivity extends AppCompatActivity {

    private LoadingPage mLoadingPage;
    private AppInfo data;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

         mLoadingPage = new LoadingPage(this){

            @Override
            public View onCreateSuccessView() {
                return HomeDetailActivity.this.onCreateSuccessView();
            }

            @Override
            public ResultState onLoad() {
                return HomeDetailActivity.this.onLoad();
            }
        };
         //直接将一个view对象设置给activity
        setContentView(mLoadingPage);
        //加载网络数据
        mLoadingPage.loadData();

    }

    public View onCreateSuccessView(){
        //初始化成功布局
        View view = UIUtils.inflate(R.layout.page_home_detail);

        //初始化应用信息模块
        FrameLayout  fl_detail_appinfo = view.findViewById(R.id.fl_detail_appinfo);
        DetailAppinfoHolder detailAppinfoHolder = new DetailAppinfoHolder();
        fl_detail_appinfo.addView(detailAppinfoHolder.getmRootView());
        detailAppinfoHolder.setData(data);

        //初始化安全描述模块
        FrameLayout fl_detail_safeinfo = view.findViewById(R.id.fl_detail_safeinfo);
        DetailSafeHolder safeHolder = new DetailSafeHolder();
        fl_detail_safeinfo.addView(safeHolder.getmRootView());
        safeHolder.setData(data);

        //初始化截图模块
        HorizontalScrollView hsvPics = view.findViewById(R.id.hsv_pics);
        DetailPicsHolder picsHolder = new DetailPicsHolder();
        hsvPics.addView(picsHolder.getmRootView());
        picsHolder.setData(data);

        //初始化描述模块
        FrameLayout fl_detail_des = view.findViewById(R.id.fl_detail_des);
        DetailDesHolder desHolder = new DetailDesHolder();
        fl_detail_des.addView(desHolder.getmRootView());
        desHolder.setData(data);

        //初始化下载模块
        FrameLayout fl_detail_download = view.findViewById(R.id.fl_detail_download);
        DetailDownloadHolder detailDownloadHolder = new DetailDownloadHolder();
        fl_detail_download.addView(detailDownloadHolder.getmRootView());
        return view;
    }

    public LoadingPage.ResultState onLoad(){
        //请求网络,加载数据
        Intent intent = getIntent();
        String packageName = intent.getStringExtra("packageName");
        HomeDetailProtocol protocol = new HomeDetailProtocol(packageName);
         data = protocol.getData(0);
         if (data!=null){
            return LoadingPage.ResultState.STATE_SUCCESS;
         }else {
             return LoadingPage.ResultState.STATE_ERROR;
         }
    }

}
